package com.group01.community.controller;


import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group01.community.entity.ZyRoom;
import com.group01.community.entity.vo.ZyRoomVo;
import com.group01.community.service.ZyRoomService;
import com.group01.community.utils.ResponseResult;
import com.group01.community.utils.ResponseStatus;
import com.group01.community.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 房间  前端控制器
 * </p>
 *
 * @author guoyuan
 * @since 2025-05-01
 */
@Api(tags = "房屋管理")
@RestController
@RequestMapping("/zyRoom")
public class ZyRoomController {
    @Autowired
    private ZyRoomService zyRoomService;

    /**
     * 分页条件查询房间信息
     * @return 房间信息
     */
    @ApiOperation("分页条件查询房间信息")
    @GetMapping("/list")
    public ResponseResult<IPage<ZyRoomVo>> getAllZyRoomByDeptId(int curr, int size, ZyRoom zyRoom) {
        try {
            IPage<ZyRoomVo> units = zyRoomService.getRoomPage(curr, size, zyRoom);
            return new ResponseResult<IPage<ZyRoomVo>>(ResponseStatus.OK, units);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseResult<IPage<ZyRoomVo>>(ResponseStatus.PARAM_ERROR, null);
    }

    /**
     * 查询所有房间信息
     * @return 房间信息
     */
    @ApiOperation("查询所有房间信息")
    @GetMapping("/listAll")
    public ResponseResult<List<ZyRoom>> getAllZyRoom() {
        try {
            return new ResponseResult<List<ZyRoom>>(ResponseStatus.OK, zyRoomService.list());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseResult<List<ZyRoom>>(ResponseStatus.INTERNAL_ERROR, null);
    }

    /**
     * 新增房间信息
     * @param zyRoom
     * @return
     */
    @ApiOperation("新增房间信息")
    @PostMapping("/add")
    public ResponseResult<String> add(@RequestBody ZyRoom zyRoom) {
        zyRoom.setRoomCode(UUIDUtils.getRoomCode());
        zyRoom.setCreateTime(LocalDateTime.now());
        zyRoomService.save(zyRoom);
        return new ResponseResult<String>(ResponseStatus.OK, "新增房间成功");
    }

    /**
     * 修改房间信息
     * @param zyRoom
     * @return
     */
    @ApiOperation("修改房间信息")
    @PutMapping("/update")
    public ResponseResult<String> update(@RequestBody ZyRoom zyRoom) {
        zyRoom.setUpdateTime(LocalDateTime.now());
        zyRoomService.updateById(zyRoom);
        return new ResponseResult<String>(ResponseStatus.OK, "修改房间成功");
    }

    /**
     * 删除房间信息
     * @param ids
     * @return
     */
    @ApiOperation("删除房间信息")
    @DeleteMapping("/delete/{ids}")
    public ResponseResult<String> delete(@PathVariable List ids) {
        zyRoomService.removeByIds(ids);
        return new ResponseResult<String>(ResponseStatus.OK, "删除房间成功");
    }

    /**
     * 导出当前页所有房间信息
     * @param response
     * @throws IOException
     */
    @ApiOperation("导出当前页所有房间信息")
    @GetMapping("/export")
    public void export(HttpServletResponse response, int curr, int size, ZyRoom zyRoom) throws IOException {
        ExcelWriter writer= null;
        ServletOutputStream outputStream= null;
        try {
            //查询当前页面所有房间
            List<ZyRoomVo> all = zyRoomService.getRoomPage(curr, size, zyRoom).getRecords();

            //在内存操作，写到浏览器
            writer = ExcelUtil.getWriter(true);

            //自定义标题别名
            writer.addHeaderAlias("unitId", "房间id");
            writer.addHeaderAlias("communityId", "小区id");
            writer.addHeaderAlias("buildingId", "楼栋id");
            writer.addHeaderAlias("roomName", "房间名称");
            writer.addHeaderAlias("roomCode", "房间编码");
            writer.addHeaderAlias("roomLevel", "楼层");
            writer.addHeaderAlias("roomAcreage", "房间面积");
            writer.addHeaderAlias("roomCost", "算费系数");
            writer.addHeaderAlias("roomStatus", "房屋状态");
            writer.addHeaderAlias("roomIsShop", "是否商品房");
            writer.addHeaderAlias("roomHouseType", "房屋户型");
            writer.addHeaderAlias("createBy", "创建者");
            writer.addHeaderAlias("createTime", "创建时间");
            writer.addHeaderAlias("remark", "备注");

            //默认配置
            writer.write(all,true);
            // 设置只导出别名列
            writer.setOnlyAlias(true);
            //设置content—type
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");

            //设置标题
            String fileName= URLEncoder.encode("房间信息","UTF-8");
            //Content-disposition是MIME协议的扩展，MIME协议指示MIME用户代理如何显示附加的文件。
            response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //将Writer刷新到OutPut
            writer.flush(outputStream,true);
            outputStream.close();
            writer.close();
        }
    }
}

